<?php

namespace app\common\logic;

use app\admin\logic\system\Config as SystemConfig;
use app\user\model\user\{Credit};

/**
 * 积分抵扣类
 * Author 亓官雨树<lucky.max@foxmail.com>
 * Date 2021-09-02
 */
class CreditRedeem
{

    protected $user_id;
    protected $user_credit;
    protected $ratio;
    protected $user_max_num;
    protected $user_max_credit;
    protected $max_num;
    protected $max_credit;
    protected $max_price;
    protected $redeem_arr;

    public function __construct($user_id, $max_price = 0)
    {
        $this->user_id = $user_id;
        $this->userCredit();
        $this->redeemRatio();
        $this->userMaxRedeemNum();
        $this->userMaxRedeemCredit();
        if ($max_price) {
            $this->max_price = intval($max_price);
            $this->creditRedeemArr();
        }
    }

    /**
     * 用户积分
     */
    protected function userCredit()
    {
        $this->user_credit = app(Credit::class)->scope('mid', $this->user_id)->value('credit') ?: 0;
    }

    /**
     * 积分抵扣比例
     */
    protected function redeemRatio()
    {
        $ratio       = SystemConfig::get('credit.redeem', [], request()->mhm_id);
        $ratio       = explode(':', $ratio['ratio']);
        $this->ratio = count($ratio) ? end($ratio) : 0;
    }

    /**
     * 用户最大可抵扣金额
     */
    protected function userMaxRedeemNum()
    {
        $this->user_max_num = intval($this->user_credit / $this->ratio);
    }

    /**
     * 用户最大可抵扣积分
     */
    protected function userMaxRedeemCredit()
    {
        $this->user_max_credit = intval($this->max_num * $this->ratio);
    }

    /**
     * 积分抵扣数组
     */
    protected function creditRedeemArr()
    {
        if ($this->user_credit <= 0 || !$this->max_price || !$this->ratio || !$this->user_max_num) {
            $this->redeem_arr = [];
        } else {
            // 最大抵扣
            $this->max_num    = min($this->user_max_num, $this->max_price);
            $this->max_credit = intval($this->max_num / $this->ratio);

            $arr[] = ['credit' => $this->max_credit, 'num' => $this->max_num];

            if ($this->max_num > 10) {
                // 10倍整数抵扣
                $decuple = bcdiv($this->max_num, 10);   // tenfold、deca-
                $base    = intval(10 * $this->ratio);

                for ($i = 1; $i <= $decuple; $i++) {
                    if (bcmul($i, 10) != $this->max_num) {
                        $arr[] = ['credit' => intval($i * $base), 'num' => intval($i * 10)];
                    }
                }

            }
            $this->redeem_arr = $arr;
        }
    }

    /**
     * 获取用户最大可抵扣金额
     */
    public function getRedeemRatio()
    {
        return $this->ratio;
    }

    /**
     * 获取用户最大可抵扣金额
     */
    public function getUserMaxRedeemNum()
    {
        return $this->user_max_num;
    }

    /**
     * 获取用户最大可抵扣金额
     */
    public function getUserMaxRedeemCredit()
    {
        return $this->user_max_credit;
    }

    /**
     * 获取实例最大可抵扣金额
     */
    public function getMaxRedeemNum()
    {
        return $this->max_num;
    }

    /**
     * 获取实例最大可抵扣金额
     */
    public function getMaxRedeemCredit()
    {
        return $this->max_credit;
    }

    /**
     * 根据抵扣金额获取抵扣积分
     */
    public function getCreditByNum($num): int
    {
        return intval($num * $this->ratio);
    }

    /**
     * 根据抵扣金额获取抵扣积分
     */
    public function getNumByCredit($credit): int
    {
        return intval($credit / $this->ratio);
    }

    /**
     * 查看抵扣金额和积分是否满足当前比例
     */
    public function isCreditMatchNum($redeem): bool
    {
        return intval($redeem['num'] * $this->ratio) == $redeem['credit'];
    }

    /**
     * 是否配置抵扣比例
     */
    public function hasRedeemRatio(): bool
    {
        return (bool)$this->ratio;
    }

    /**
     * 设置最大可抵扣金额
     */
    public function setMaxPrice($price)
    {
        $this->max_price = $price;
    }

    /**
     * 获取用户积分抵扣数组
     */
    public function getCreditRedeemArr()
    {
        return $this->redeem_arr;
    }

}
